AsgDetallePoliza = function(){
	return {
		initConfig: function(config){
			Ext.define("asgDetallePolizaModel", {
			    extend: "Ext.data.Model",
			    fields: [
		             "asgClienteId", "asgPolizaId",
			         "asgCliente", "asgPolizaNumero", "asgPolizaAseguradora", 
			         "asgPolizaTipoSeguro", "asgPolizaAsesor",
			         "asgPolizaFechaInicio", "asgPolizaFechaFinalizacion"
		        ]
			});
			var asgDetallePolizaStore = Ext.create("Ext.data.Store", {
				autoLoad: {params:{start: 0, limit: 50}},
				pageSize: Asg.constants.PAGE_SIZE,
				model: "asgDetallePolizaModel",
				proxy: {
			        type: "ajax",
			        url: Asg.constants.CONTEXT_PATH + "/seguros/doSearch",
			        reader: {
			        	type: "array",
			        	root: "list",
			        	totalProperty: "totalResult"
	        		}
			    },
			    listeners: {
			    	beforeload: function(store, operation, options){
			    		if(!operation) operation = {};
			    		if(!operation.params) operation.params = {};
			    		var form = Ext.getCmp("asgDetallePolizaForm").getForm();
			    		if(!form.isValid()){
			    			Asg.Message.alertInvalidFields();
			    			return false;
			    		}
			    		var values = form.getValues();
			    		for(value in values){
			    			operation.params["asgSeguroForm." + value] = values[value];
			    		}
			    		if(operation.params.restart){
			    			operation.start = 0;
			    			operation.page = 1;
			    		}
			    	}
			    	
			    },
			    groupers: [{property: "asgCliente"}]
			});
			return {
				menu: [
			       {
	                	xtype: "buttongroup",
	                	title: "CLIENTES",
	                	defaults: {iconAlign: "left"},
	                	padding:"5 5 5 5",
	                	style: "min-width: 200px;",
			            bodyStyle:"min-height:25px",
	                	items: [
							{
								text: "Crear Cliente", 
								iconCls: Asg.constants.ADD_ICON,
								hidden: config.isReadOnly,
								menu: [
							        {
							        	text: "Persona Natural",
							        	handler:Commons.createCliente
							        },{
							        	text: "Persona Juridica",
							        	handler:Commons.createClienteJuridico
							        }
							    ]
							},
	    			        {xtype: 'tbspacer', maxWidth: 5, minWidth: 5, width: 5},
	            	        {text: "Ver Datos", iconCls: Asg.constants.OPEN_ICON, handler:Commons.openPoliza},
	    			        {xtype: 'tbspacer', maxWidth: 5, minWidth: 5, width: 5}
	    	            ]
	                },
	                "->",
	                Viewport.getMenuHome()
                ],
				items: [ 
			        {
			        	xtype: "form",
			        	id: "asgDetallePolizaForm",
			        	height: 125,
			        	bodyStyle: "background: #DFE8F6",
			        	layout: "fit",
			        	border: false,
						items: {
							xtype: "fieldset",
							title: "PARAMETROS DE BUSQUEDA",
							style: "background: #FFFFFF;",
							layout: {
	        	        		type: "table",
								columns: 6,
								tableAttrs: {
						            style: {width: "740px"},
						            align: "center"
								}
	        	        	},
	        	        	defaults: Asg.Form.defaults({width: 240, labelAlign: "top", colspan: 2}),
	        	        	items: [
	    	        	        {
	    	        	        	xtype: "textfield",
	    	        	        	id: "asgPolizaNumero",
	    	        	        	name: "asgPolizaNumero",
	    	        	        	fieldLabel: "No. Poliza",
	    	        	        	width: 110
	    	        	        },{
	    	        	        	xtype: "datefield",
	    	        	        	id: "asgFechaInicio1",
	    	        	        	name: "asgFechaInicio1",
	    	        	        	fieldLabel: "Fecha Inicio",
	    	        	        	width: 110,
	    	        	        	submitFormat: "Ymd",
	    	        	        	colspan: 1
	    	        	        },{
	    	        	        	xtype: "datefield",
	    	        	        	id: "asgFechaInicio2",
	    	        	        	name: "asgFechaInicio2",
	    	        	        	fieldLabel: " ",
	    	        	        	labelSeparator: "&nbsp;",
	    	        	        	width: 105,
	    	        	        	submitFormat: "Ymd",
	    	        	        	colspan: 1
	    	        	        },{
	    	        	        	xtype: "datefield",
	    	        	        	id: "asgFechaFinalizacion1",
	    	        	        	name: "asgFechaFinalizacion1",
	    	        	        	fieldLabel: "Fecha Finalizaci\u00F3n",
	    	        	        	width: 110,
	    	        	        	submitFormat: "Ymd",
	    	        	        	colspan: 1
	    	        	        },{
	    	        	        	xtype: "datefield",
	    	        	        	id: "asgFechaFinalizacion2",
	    	        	        	name: "asgFechaFinalizacion2",
	    	        	        	fieldLabel: " ",
	    	        	        	labelSeparator: "&nbsp;",
	    	        	        	width: 105,
	    	        	        	submitFormat: "Ymd",
	    	        	        	colspan: 1
	    	        	        },{
	    	        	        	xtype: "textfield",
	    	        	        	id: "asgClienteNombreCompleto",
	    	        	        	name: "asgClienteNombreCompleto",
	    	        	        	fieldLabel: "Cliente / Raz\u00F3n Social / Nombre Comercial"
	    	        	        },{
	    	        	        	xtype: "combo",
	    	        	        	id: "ctgAseguradoraId",
	    	        	        	name: "ctgAseguradoraId",
	    	        	        	fieldLabel: "Aseguradora",
	    	        	        	store: new Ext.data.SimpleStore({
    	        	        			data: config.ctgAseguradoras || [],
    	        	        			fields: ["id", "value"]
    	        	        		}),
    	        	        		displayField: "value",
    	        	        		valueField: "id",
    	        	        		listeners: {
    	        	        			select: function(field, record){
    	        	        				if(record != null && record.length > 0){
    	        	        					var data = Asg.Combo.filter(record[0].get("id"), config.ctgAsesores, 2);
    	        	        					Asg.Combo.loadData("ctgAsesorId", data, false);
    	        	        				}else if(Ext.isEmpty(this.getValue())){
    	        	        					Asg.Combo.removeAll("ctgAsesorId", false);
    	        	        				}
    	        	        			}
    	        	        		}
	    	        	        },{
	    	        	        	xtype: "combo",
	    	        	        	id: "ctgAsesorId",
	    	        	        	name: "ctgAsesorId",
	    	        	        	fieldLabel: "Asesor",
	    	        	        	store: new Ext.data.SimpleStore({
    	        	        			data: [],
    	        	        			fields: ["id", "value"]
    	        	        		}),
    	        	        		displayField: "value",
    	        	        		valueField: "id"
	    	        	        }
		        	        ]
						}
			        },{
			        	xtype: "grid",
			        	flex: 1,
			        	frame: true,
			        	minHeight: 300,
			        	margins: "5 0 0 0",
			        	store: asgDetallePolizaStore,
			        	columns: [
	        	           {
	        	        	   xtype:"actioncolumn",
	        	        	   width: 30,
	        	        	   hidden: false,
	        	        	   items: [
        	        	           {
        	        	        	   icon: Asg.constants.CONTEXT_PATH + "/resources/images/open.png", 
        	        	        	   iconCls: "icon", 
        	        	        	   handler: function(grid, rowIndex, colIndex) {
        	        	        		   var rows = [grid.getStore().getAt(rowIndex)];
        	        	        		   AsgSeguros.setAsgClienteId(rows[0].get("asgClienteId"));
   	        	            			   AsgSeguros.setAsgPolizaId(rows[0].get("asgPolizaId"));
        	        	        		   Commons.openPoliza();
        	        	        	   }
        	        	           }
    	        	           ]
	        	           },
	        	           {header: "No. Poliza", dataIndex: "asgPolizaNumero", width: 140},
	        	           {header: "Tipo de Seguro", dataIndex: "asgPolizaTipoSeguro", width: 160},
	        	           {header: "Aseguradora", dataIndex: "asgPolizaAseguradora", flex: 1, minWidth: 150},
	        	           {header: "Asesor", dataIndex: "asgPolizaAsesor", flex: 1, minWidth: 225},
	        	           {header: "Fecha Inico", dataIndex: "asgPolizaFechaInicio", width: 100, renderer: "webFormatDateYmd_d_m_Y"},
	        	           {header: "Fecha Finalizaci\u00F3n", dataIndex: "asgPolizaFechaFinalizacion", width: 110, renderer: "webFormatDateYmd_d_m_Y"}
        	            ],
        	            selModel:{
	        	            listeners: {
	        	            	selectionchange: function(selectionModel, rows){
	        	            		if(rows && rows.length > 0){
	        	            			AsgSeguros.setAsgClienteId(rows[0].get("asgClienteId"));
	        	            			AsgSeguros.setAsgPolizaId(rows[0].get("asgPolizaId"));
	        	            		}
	        	            	}
	        	            }
        	            },
        	            features: [
				           {
				        	   ftype: "grouping",
				        	   enableNoGroups: false,
				        	   startCollapsed: true,
				        	   groupHeaderTpl: '{name} -> {rows.length} Poliza{[values.rows.length > 1 ? "s" : ""]}'
				           }
			            ],
	                    dockedItems: [
                           {
                        	   xtype: "toolbar",
                        	   dock: "top",
                        	   items: [
                    	           "->",
                    	           {
                    	        	   text: "Buscar", 
                    	        	   iconCls: Asg.constants.SEARCH_ICON,
                    	        	   handler: function(){
                    	        		   asgDetallePolizaStore.currentPage = 1;
                    	        		   asgDetallePolizaStore.load({params:{restart: true}});
                    	        		   AsgSeguros.setAsgClienteId(null);
                    	        		   AsgSeguros.setAsgPolizaId(null);
                    	        	   }
                	        	   },{
                    	        	   text: "Borrar Filtros", 
                    	        	   iconCls: Asg.constants.CANCEL_ICON,
                    	        	   handler: function(){
                    	        		   Ext.getCmp("asgDetallePolizaForm").getForm().reset();
                    	        	   }
                	        	   }
                	           ]
                           },{
                        	   xtype: "pagingtoolbar",
                        	   store: asgDetallePolizaStore,
                        	   dock: "bottom",
                        	   displayInfo: true
			               }
                        ]
			        }
				]
			};
		}
	};
}();